package com.thread.concurrentTest;

import org.junit.Test;

import java.util.concurrent.*;

public class CallableTest {

    @Test
    public void test0() throws ExecutionException, InterruptedException {
        ThreadLocal<Object> threadLocal = new ThreadLocal<>();

        FutureTask<Object> futureTask = new FutureTask<Object>(() -> {
            Thread.sleep(1000);
            return Thread.currentThread().getName();
        });

        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }

    @Test
    public void test1() throws ExecutionException, InterruptedException {
        ThreadLocal<Object> threadLocal = new ThreadLocal<>();

        Callable<Object> callable = () -> {
            Thread.sleep(10000);
            return Thread.currentThread().getName();
        };

        FutureTask<Object> futureTask = new FutureTask<Object>(callable);
        FutureTask<Object> futureTask2 = new FutureTask<Object>(callable);

//        try {
//            System.out.println(futureTask.get(10, TimeUnit.SECONDS));
//        } catch (TimeoutException e) {
//            e.printStackTrace();
//        }

//        futureTask.run();
//        System.out.println(futureTask.get());
//
//        new Thread(futureTask).start();
//        System.out.println(futureTask.get());

        new Thread(futureTask2).start();
        System.out.println(futureTask2.get());

        futureTask2.run();
        System.out.println(futureTask2.get());
    }

}
